import exceed from 'utils/apimap';
import utils from 'utils';
import intl from '../locale';
import { navigate } from 'utils/router';

const { organization, pathResource, resourceSubject, codeReviewDetail } = window.FORCE;
const { id: org_id } = organization; // 获取组内库

// 查询用户引导点状态
// isOnlyOnce 只会改变一次时，改成false时，将数据保存到localStorage
export function getIntroPointState(type, others, isOnlyOnce = true) {
  return new Promise((resolve, reject) => {
    if (type === 'codeup_common_pop_up') {
      isOnlyOnce = false;
    }
    const data = {
      type,
      ...others,
    };

    // 如果key和企业相关，则将缓存key带上orgId
    const isOrgAssociated = [
      'org_member_name',
      'rdc_newbie_task',
      'rdc_invite_organization_member',
      'welcome_to_codeup_pop',
      'sensitive_behavior_guiding_pop',
      'create_your_project_pop',
      'query_user_first_entry',
    ].includes(type);
    const cacheKey = { ...data };
    if (isOrgAssociated) {
      cacheKey.orgId = org_id;
    }

    if (isOnlyOnce && utils.getCachedData(JSON.stringify(cacheKey)) === 'false') {
      resolve({
        state: false,
      });

      return;
    }
    exceed
      .fetch({
        api: 'introPointState.get',
        data,
      })
      .then((result) => {
        if (isOnlyOnce && !result.state) {
          utils.setCachedData(JSON.stringify(cacheKey), 'false');
        }
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 设置用户引导点状态（直接关闭）
export function setIntroPointState(type, others) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'introPointState.set',
        params: {
          type,
        },

        data: others,
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

export function getProjectMembers(projectId, data) {
  return new Promise((resolve, reject) => {
    pathResource.type = pathResource.type || '';
    const pathResourceType = pathResource.type.toLocaleLowerCase() || 'project';
    const isProject = pathResourceType === 'project';
    exceed
      .fetch({
        api: `members.${pathResourceType}.get`,
        params: { [isProject ? 'projectId' : 'groupId']: projectId || pathResource.id },
        data: {
          ...data,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

export function getAllMembersByGroup() {
  return [];
}

// 获单个组信息
export function getSingleGroupInfo(groupId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'groups.single',
        params: {
          id: groupId,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取管理员列表 @params id 组织空间的id
export function getAdminList() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'organization.admin.get',
        params: {
          org_id,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 添加管理员 @params id 组织空间的id
export function addAdmin(data) {
  return new Promise((resolve, reject) => {
    // url:api/v4/organization/{org_id}/admin post
    exceed
      .fetch({
        api: 'organization.admin.post',
        params: {
          org_id,
        },

        data: {
          tb_user_id: data.tb_user_id.join(','),
          access_level: data.access_level,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 管理员降级 @params id 组织空间的id
export function deleteAdmin(adminId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'organization.admin.delete',
        params: {
          org_id,
          id: adminId,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取所有文件

export function getAllFiles() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.tree',
        params: { projectId: pathResource.id },
        data: {
          type: 'RECURSIVE', // 循环取所有文件
          ref_name: resourceSubject.revision,
          path: '', // 所有文件，不传数据
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取commits列表
export function getCommitsList(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.commits_with_user',
        params: { projectId: pathResource.id },
        data: {
          ...params,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 根据commitId查询自动化扫描结果列表
export function getCommitsBuilds(commitId, projectId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.builds.commits',
        params: {
          projectId: pathResource.type === 'PROJECT' ? pathResource.id : projectId,
          commitId,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

export function getMrBuilds(mrId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.mr.builds',
        params: { projectId: pathResource.id, mrId },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取单个commit信息
export function getCommitInfoById(params, projectId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.commit.info',
        params: {
          projectId: pathResource.type === 'PROJECT' ? pathResource.id : projectId,
          sha: params.sha,
        },
        data: {
          ...params,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取变更文件
export function getDiffs(params, projectId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.commit.diff.noErrorToast',
        params: {
          projectId: pathResource.type === 'PROJECT' ? pathResource.id : projectId,
          sha: params.sha,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取更多行
export function getMoreLines(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.blobs.v4',
        params: { projectId: pathResource.id },
        data: {
          filepath: params.diff.newPath,
          from: params.from,
          to: params.to,
          ref: params.commitId,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 根据commit id获取测试结果
export function getTestResult(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.testResult',
        params: { projectId: pathResource.id, commitId: params.commitId },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 根据commitId获取评论
export function getCommentListByCommitId(params, projectId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.commit.comments.get',
        params: {
          commitId: params.sha,
          projectId: pathResource.type === 'PROJECT' ? pathResource.id : projectId,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// commits发表评论
export function postComment(params, projectId) {
  return new Promise((resolve, reject) => {
    const { sha, ...data } = params;
    exceed
      .fetch({
        api: 'projects.commit.comments.post',
        params: {
          commitId: params.sha,
          projectId: pathResource.type === 'PROJECT' ? pathResource.id : projectId,
        },
        data: {
          ...data,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// commits分支及tag信息
export function getBranchAndTag(params, projectId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.commit.refs',
        params: {
          commitId: params.sha,
          projectId: pathResource.type === 'PROJECT' ? pathResource.id : projectId,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

export function getBranchInfoByBranchName(branchName) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.branch',
        params: {
          projectId: pathResource.id,
          branch: branchName,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 创建tag
export function createTag(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.tags.post',
        params: { projectId: pathResource.id },
        data: { ...params },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取对比文件的commits、diffs
export function getCompareInfo(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.commit.compare',
        params: { projectId: pathResource.id },
        data: {
          merge_base: true,
          max_diff_file: 500,
          max_diff_byte: 2097152,
          ...params,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 拿cookie换privateToken，主搜接口需要
export function getToken() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'internal.exchange',
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取搜索数据
export function getSearchData({
  searchText = '',
  searchType = 'commits',
  sortBy = 'default|desc',
  privateToken,
  currentPage,
  pageSize,
}) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'search.content',
        data: {
          q: searchText,
          type: searchType,
          order: sortBy.split('|')[0],
          sort: sortBy.split('|')[1],
          private_token: privateToken,
          page: currentPage,
          page_size: pageSize,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

/**
 * {{project}}/settting 页面接口
 *
 */
// 库名称和路径校验
export function checkProjectName(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.verify',
        data: {
          namespace_id: params.namespace_id || organization.namespace_id,
          name: params.name || '',
          path: params.path || '',
          contains_recycled: params.contains_recycled || false,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}
// 库大小
export function getProjectSize() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.size',
        params: { projectId: pathResource.id },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}
// 获取库GC
export function getProjectGC() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.gc.get',
        params: { projectId: pathResource.id },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}
// 执行库GC
export function postProjectGC(immediately) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.gc.post',
        params: { projectId: pathResource.id, immediately },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

/**
 * 分支设置页面
 */
// 设置默认分支
export function postDefaultBranch(value) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.default_branches.put',
        params: { projectId: pathResource.id },
        data: {
          default_branch: value,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 保护分支搜索
export function searchProtectedBranches(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.protect_branches.search',
        params: { projectId: pathResource.id },
        data: {
          ...params,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 搜索分支，无参数获取所有分支
export function getAllBranches(projectId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.all.branches.search',
        params: { id: pathResource.type === 'PROJECT' ? pathResource.id : projectId },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取默认分支
export function getDefaultBranch(projectId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.default_branches.get',
        params: { projectId: pathResource.type === 'PROJECT' ? pathResource.id : projectId },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取保护分支列表
export function getProtectedBranchesList() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.protect_branches.list',
        params: { projectId: pathResource.id },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}
// 删除保护分支
export function deleteProtectedBranch(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.protect_branches.delete',
        params: { projectId: pathResource.id, id: params.id },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}
// 修改保护分支
export function updateProtectedBranch(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.protect_branches.put',
        params: { projectId: pathResource.id, id: params.id },
        data: {
          ...params,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}
// 新增保护分支
export function addProtectedBranch(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.protect_branches.post',
        params: { projectId: pathResource.id },
        data: {
          ...params,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

/**
 * MR页面
 */
// 获取左侧树结构
export function getMrChangeTree(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.mr.getPushRecordsChangeTree',
        params: {
          projectId: pathResource.id,
          mrId: codeReviewDetail ? codeReviewDetail.id : '',
        },

        data: {
          from_commit: params.from_commit,
          to_commit: params.to_commit,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取评审状态
const state = {
  approve_status: 'NO_ACCESS',
  message: intl.get({
    id: 'code-assets.src.api.YouAreNotAuthorizedTo',
    defaultMessage: '您无权限通过目标分支为保护分支的代码评审',
  }),
};

export function getApproveStatusAccess() {
  return new Promise((resolve, reject) => {
    console.log(resolve, reject);
    setTimeout(() => {
      resolve(state);
    }, 200);
    // exceed
    //   .fetch({
    //     api: 'project.mr.approve_status',
    //     params: {
    //       projectId: pathResource.id,
    //       mrId: params.mrId,
    //     },
    //   })
    //   .then((result) => {
    //     resolve(result);
    //   }).fail((err) => {
    //     reject(err);
    //   });
  });
}
// 获取右侧diff内容
export function getMrChangeDiff(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.mr.getPushRecordsChangesetsDiff',
        params: {
          projectId: pathResource.id,
          mrId: codeReviewDetail.id,
        },

        data: { ...params },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取评论的接口
export function getCommentList(queryType) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.mr.logs.summary',
        params: {
          projectId: pathResource.id,
          mrId: codeReviewDetail.id,
        },

        data: {
          queryType,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取commits区间的接口以及提交历史的接口,无分页
export function getMrCommits() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.mr.commits.all',
        params: {
          projectId: pathResource.id,
          mrId: codeReviewDetail.id,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

//  查看评审我的草稿列表
// const { allDrafts, allComments } = mock;
export function getAllDrafts(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.mr.drafts.get',
        params: {
          projectId: pathResource.id,
          mrId: codeReviewDetail.id,
        },

        data: {
          ...params,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 查看评审用户评论列表
export function getAllComments(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.mr.comments.get',
        params: {
          projectId: pathResource.id,
          mrId: codeReviewDetail.id,
        },

        data: {
          from_commit: params.from_commit,
          to_commit: params.to_commit,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 批量提交草稿
export function postAllDrafts(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.mr.drafts.post',
        params: {
          projectId: pathResource.id,
          mrId: codeReviewDetail.id,
        },

        data: {
          note: params.note || null,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 提交单个草稿或者评论（含草稿）
export function postSingleComment(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.mr.commentOrDraft.post',
        params: {
          projectId: pathResource.id,
          mrId: codeReviewDetail.id,
        },

        data: {
          ...params,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

/**
 * 库内安全扫描设置
 *  */
// 查询
export function getProjectServices() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.service.get',
        params: {
          projectId: pathResource.id,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}
// 修改
export function saveProjectServices(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.service.put',
        params: {
          projectId: pathResource.id,
        },

        data: {
          integration_service_setting: params.integration_service_setting,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 集成服务开启和关闭记录动态
export function postServiceLog(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.service.access',
        params: {
          projectId: pathResource.id,
        },

        data: {
          service: params.service,
          authorized: params.authorized,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

export function checkAliasName(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'organization.verify',
        data: { path: params },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取webIDE入口
export function getWebIdeUrl(params, projectId) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.webIde',
        params: {
          projectId: projectId || pathResource.id,
        },

        data: {
          ...params,
          app: 'webIDE',
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 代码组名称和路径重复校验
export function checkGroupNamePath(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'group.verify',
        data: {
          namespace_id: params.namespace_id || organization.namespace_id,
          name: params.name || '',
          path: params.path || '',
          contains_recycled: params.contains_recycled || false,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 判断是否可以转移组：1、无权限（企业设置、代码组设置） 2、名称路径重复 3、受父组公开性影响
export function checkCanTranfer(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'groups.verify.can_transfer',
        data: {
          namespace_id: params.namespace_id,
          project_id: params.project_id || '',
          group_id: params.group_id || '',
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取组下加密库列表
export function getEncryptedProjectsInGroup(params, data) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'group.encrypted_projects.list',
        params,
        data,
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取企业下加密库列表
export function getAllEncryptedProjects(params, data) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'org.cryptos.single.projects',
        params,
        data,
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 导入库校验是否创建库，路径dai
export function checkImportProjectName(params) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.import.verify',
        data: {
          namespace_id: params.namespace_id || organization.namespace_id,
          name: params.name || '',
          path: params.path || '',
          visibility_level: params.visibility_level,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

export function checkGlobalNewbieTaskStatus(key) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'global.newbie.task.switch',
        params: {
          key,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取最近一次提交
export function getRecentPushApi(repoId, data) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'project.recent.push',
        params: {
          repoId,
        },

        data,
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 开启流水线
export function openPinpoint(data) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.pinpoint.new',
        params: {
          projectId: pathResource.id,
        },

        data,
      })
      .then((result) => {
        if (result.successful) {
          resolve();
        } else {
          reject();
        }
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取MR的baseId
export function getMergeBase(params = {}, data = {}) {
  return new Promise((resolve) => {
    exceed
      .fetch({
        api: 'project.mr.merge_base',
        params,
        data,
      })
      .then((result) => {
        resolve(result);
      })
      .catch((err) => {
        if (err.status === 400) {
          const { origin, pathname } = window.location;
          const pathnameArr = pathname.split('/');
          const tag = pathnameArr.pop();
          if (tag === 'changes') {
            navigate(`${origin}${pathnameArr.join('/')}`);
          }
        }
      });
  });
}

// 获取企业设置的加密设置
export function getOrgCrypto() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'org.cryptos.crypto_settings.get',
        params: {
          org_id: organization.id,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取库设置的加密设置
export function getRepoCrypto() {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.single.crypto.get',
        params: {
          repoId: pathResource.id,
        },
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 修改库设置的加密设置
export function updateRepoCrypto(data) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'projects.single.crypto.put',
        params: {
          repoId: pathResource.id,
        },

        data,
      })
      .then((result) => {
        resolve(result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}

// 获取企业的成员信息
export function getOrgMembers(organization_id) {
  return new Promise((resolve, reject) => {
    exceed
      .fetch({
        api: 'organization.member',
        params: {
          orgId: organization_id,
        },
      })
      .then((result) => {
        resolve(result.result);
      })
      .fail((err) => {
        reject(err);
      });
  });
}


/**
 * @description uiless-member自定义请求
 * @param data {object} 参数
 */

 export function customMemberSearchAll(data = {}) {
  return new Promise((resolve, reject) => {
    exceed.fetch({
      api: 'memberSearchVpc',
      params: {
        id: window.FORCE.organization.organization_id,
      },
      data,
    }).then(res => {
      res.shift(1);
      resolve(res.map((item) => {
        return {
          ...item,
          memberType: 'user',
        };
      }));
    }).fail((err) => {
      reject(err);
    });
  });
}
